package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.ObjectUtils;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.domain.*;
import com.jumi.microservice.service.GoodsService;
import com.jumi.microservice.service.LabelService;
import com.jumi.microservice.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签管理controller组件
 *
 * @author Administrator
 */
@Api(tags = "标签管理相关接口")
@RestController
@RequestMapping("/commodity/label")
public class LabelController extends BaseController {

    /**
     * 标签管理service组件
     */
    @Autowired
    private LabelService labelService;

    /**
     * 商品管理service组件
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * 日志service组件
     */
    @Autowired
    private LogService logService;

    /**
     * 标签列表分页查询查询
     *
     * @param labelName 标签名称
     * @return 标签VO集合
     */
    @ApiOperation(value = "标签列表分页查询查询", notes = "标签列表分页查询查询")
    @GetMapping("/list")
    public BasePageResponse<List<LabelVO>> listLabel(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam String labelName) {
        List<LabelVO> labelVoList = convertLabelDtoList2VoList(labelService.listLabel(labelName));
        return ResultGenerator.genSuccessResultPage(
                labelVoList.stream()
                        .skip(pageSize * (pageNum - 1))
                        .limit(pageSize).collect(Collectors.toList())
                , Long.valueOf(labelVoList.size()));
    }

    /**
     * 根据标签id查询标签
     *
     * @param id 标签id
     * @return 标签Vo
     */
    @ApiOperation(value = "查询标签", notes = "获取指定ID标签数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/detail/{id}")
    public ResponseResult<LabelVO> getLabelById(@PathVariable Long id) {
        LabelDTO labelDto = labelService.getLabelById(id);
        return ResultGenerator.genSuccessResult(convertLabelDto2Vo(labelDto));
    }

    /**
     * 新增标签
     *
     * @param label 商品标签
     * @return 处理结果, 新增成功返回true
     */
    @ApiOperation(value = "新增标签", notes = "新增标签表数据")
    @PostMapping("/add")
    @WebLog(description = "新增标签")
    public ResponseResult<Boolean> saveLabel(@Validated @RequestBody LabelVO label, HttpServletRequest request) {
        labelService.saveLabel(convertLabelVo2Dto(label), request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 更新标签
     *
     * @param label   商品标签
     * @param request HttpServletRequest
     */
    @ApiOperation(value = "更新标签", notes = "更新标签表数据")
    @PutMapping("/update")
    @WebLog(description = "更新标签")
    public ResponseResult<Boolean> updateLabel(@Validated @RequestBody LabelVO label, HttpServletRequest request) {
        labelService.updateLabel(convertLabelVo2Dto(label), request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 删除标签
     *
     * @param id      标签id
     * @param request HttpServletRequest
     */
    @ApiOperation(value = "删除标签", notes = "删除指定ID标签数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @DeleteMapping("/delete/{id}")
    @WebLog(description = "删除标签")
    public ResponseResult<Boolean> deleteLabelById(@PathVariable Long id, HttpServletRequest request) {
        labelService.removeLabelById(id, request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 标签状态启用/停用
     *
     * @param id     标签id
     * @param status 状态 (1启用，0.禁用)
     * @return 处理结果, 更新成功返回true
     */
    @ApiOperation(value = "标签状态启用/停用", notes = "标签状态的启用/停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    @PutMapping("/enabled")
    @WebLog(description = "标签状态启用/停用")
    public ResponseResult<Boolean> enableStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status, HttpServletRequest request) {
        labelService.enableStatus(id, status, request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 标签下拉列表查询
     *
     * @return 标签VO集合
     */
    @ApiOperation(value = "添加商品-标签列表下拉查询", notes = "添加商品-标签列表下拉查询")
    @GetMapping("/dropListLabel")
    public ResponseResult<List<LabelVO>> dropListLabel() {
        List<LabelVO> labelVoList = convertLabelDtoList2VoList(labelService.dropListLabel());
        return ResultGenerator.genSuccessResult(labelVoList);
    }


    /**
     * 标签绑定的商品SPU列表查询
     *
     * @param labelId 标签id
     * @return 标签VO集合
     */
    @ApiOperation(value = "标签绑定的商品SPU列表查询", notes = "标签绑定的商品SPU列表查询")
    @GetMapping("/goods/list")
    public BasePageResponse<List<GoodsLabelVO>> listGoodsByLabelId(@RequestParam("labelId") Long labelId,
                                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        List<GoodsLabelVO> goodsLabels = convertGoodsDtoList2VoList(goodsService.listGoodsByLabelId(labelId));
        return ResultGenerator.genSuccessResultPage(
                goodsLabels.stream()
                        .skip(pageSize * (pageNum - NumberConstant.NUMBER_ONE.intValue()))
                        .limit(pageSize).collect(Collectors.toList())
                , Long.valueOf(goodsLabels.size()));
    }

    /**
     * 操作日志分页查询
     *
     * @param operateLogQuery 账号查询条件
     * @return 账号操作日志集合
     */
    @ApiOperation(value = "标签操作日志分页查询", notes = "标签操作日志分页数据")
    @GetMapping(value = "/log")
    public BasePageResponse<List<LogDetailVO>> lisOperateLogByPage(OperateLogQuery operateLogQuery) {
        BasePageResponse<List<LogDetailVO>> responseResult = logService.listOperateLogs(operateLogQuery);
        return responseResult;
    }


    /**
     * 将标签VO对象转换为DTO对象
     *
     * @param label 标签VO
     * @return LabelDTO 标签DTO
     */
    private LabelDTO convertLabelVo2Dto(LabelVO label) {
        LabelDTO targetLabel = null;
        try {
            targetLabel = label.clone(LabelDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetLabel;
    }

    /**
     * 将标签DTO对象转换为VO对象
     *
     * @param label 标签DTO
     * @return LabelVO 标签vo
     */
    private LabelVO convertLabelDto2Vo(LabelDTO label) {
        LabelVO targetLabel = null;
        try {
            targetLabel = label.clone(LabelVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetLabel;
    }

    /**
     * 将标签DTO对象集合转换为VO对象集合
     *
     * @param labels 标签DTO集合
     * @return List<LabelVO> 标签VO对象集合
     */
    private List<LabelVO> convertLabelDtoList2VoList(List<LabelDTO> labels) {
        List<LabelVO> labelVoList = null;
        try {
            labelVoList = ObjectUtils.convertList(labels, LabelVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return labelVoList;
    }

    /**
     * 将商品DTO对象集合转换为VO对象集合
     *
     * @param goods 商品DTO集合
     * @return List<GoodsLabelVo> 商品VO对象集合
     */
    private List<GoodsLabelVO> convertGoodsDtoList2VoList(List<GoodsDTO> goods) {
        List<GoodsLabelVO> goodsLabelVoList = null;
        try {
            goodsLabelVoList = ObjectUtils.convertList(goods, GoodsLabelVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return goodsLabelVoList;
    }
}
